<?php if ( ! defined( 'BASEPATH' ) ) exit( 'No direct script access allowed' );
/**
 * BackendPro
 *
 * A website backend system for developers for PHP 4.3.2 or newer
 *
 * @package         conlabz CMS
 * @author          Christian Schneider
 * @copyright       Copyright (c) 2009
 * @license         
 * @link            http://www.conlabz.de
 * @filesource
 */

// ---------------------------------------------------------------------------

/**
 * Location_model
 *
 * Model used to retrive webites
 *
 * @package			conlabz CMS
 * @subpackage		Models
 */
class Location_model extends Base_model {
	function Location_model( ) {
		parent::Base_model( );

		$this->_TABLES = array( 'Option' => $this->config->item( 'backendpro_table_prefix' ) . 'article' );

		// Cache to store already fetched items
		$this->_CACHE = array( );

		// Object keyword
		// I wouldn't advise changing this, it could corrupt current
		// articles, the reason for needing this is to stop unserialze
		// errors spamming the log files
		$this->object_keyword = "BeP::Object::";

		log_message( 'debug', 'conlabz CMS : Events_model class loaded' );
	}

	/**
	 * Get Events
	 *
	 * @access public
	 * @param mixed $where Where query string/array
	 * @param array $limit Limit array including offset and limit values
	 * @return object
	 */
	function getLocations( $where = NULL, $limit = array( 'limit' => NULL, 'offset' => '' ) ) {
		$this->db->select( 'location.id, 
							location.title,
							location.description,
							location.street,
							location.zipcode,
							location.city,
							location.latitude,
							location.longitude' );
		
		$this->db->from( "location" );

		if( !is_null( $where ) ) {
			$this->db->where( $where );
		}
		if( !is_null( $limit['limit'] ) ) {
			$this->db->limit( $limit['limit'], ( isset( $limit['offset'] ) ? $limit['offset'] : '' ) );
		}
		$this->db->order_by('location.id desc'); 
		return $this->db->get( );
	}
	
	/**
	 * Insert new Location
	 *
	 * @access public
	 * @param array $data Data array
	 */
	function insert( $data = array( ) ) {
		//get user_ref from session object
		$data['user_ref'] = $this->session->userdata( 'id' );
		
		//create new events entry
		$this->db->set( 'id',          $data['id']          );
		$this->db->set( 'city', 	   $data['city'] 	    );
		$this->db->set( 'title',   	   $data['title']       );
		$this->db->set( 'street', 	   $data['street'] 	    );
		$this->db->set( 'zipcode', 	   $data['zipcode']     );
		$this->db->set( 'latitude',    $data['latitude']    );
		$this->db->set( 'longitude',   $data['longitude']   );
		$this->db->set( 'description', $data['description'] );

		//insert the new events in the events DB
		$this->db->insert( 'location' );
		
		return $this->db->insert_id( );
	}
	
	/**
	 * Update location
	 *
	 * @access public
	 * @param array $data Data array
	 */
	function update( $data = array( ), $id ) {		
		$location = array( );
		$location['city'] 	     = $data['city'];
		$location['title']       = $data['title'];
		$location['street'] 	 = $data['street'];
		$location['zipcode'] 	 = $data['zipcode'];
		$location['latitude']    = $data['latitude'];
		$location['longitude']   = $data['longitude'];
		$location['description'] = $data['description'];
	
		//insert the new event
		$this->db->update( 'location', $location, array( 'id' => $id ) );
	}
	
	/**
	 * Constructs an array of the given locations to be used for a dropdown menu
	 *
	 * @access public
	 * @return array
	 */
	function buildLocationsDropdown( ) {
		$location = $this->getLocations( );
		
		$dropdown = array( );
		foreach( $location->result( ) as $row ) {
	    	 $dropdown[$row->id] = $row->title;
		}
		
		return $dropdown;
	}
}
/* End of file location_model.php */
/* Location: ./modules/events/models/location_model.php */